// DatSaver.cpp: implementation of the CDatSaver class.
//
//////////////////////////////////////////////////////////////////////


#include "DatSaver.h"


//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CDatSaver::CDatSaver()
{
	IsClosed	=	true;
}

CDatSaver::~CDatSaver()
{
	if (BinFile.IsOpened())
	{
		PleaseWait PleaseWaitDlg(NULL);
		PleaseWaitDlg.Show();
		
		Close();
		if(BinFile.IsOpened()) BinFile.Close();
		if(DatFile.IsOpened()) DatFile.Close();
		if(LogFile.IsOpened()) LogFile.Close();
		if(AlgoFile.IsOpened()) AlgoFile.Close();
	
		PleaseWaitDlg.Hide();
	}
}
void CDatSaver::InstrumentDescriptor(wxString InstrumentName,int SampleFreq,wxString *pCHLabels,int NumberOfChannels)
{
	wxString Temp;
	LogFile.AddLine(InstrumentName);
//	LogFile.AddLine($(ProjectName));
	Temp = wxString::Format(_("Sample Frequency\t%d"),SampleFreq);
	LogFile.AddLine(Temp);
	Temp = wxString::Format(_("Number of channels\t%d"),NumberOfChannels);
	LogFile.AddLine(Temp);
	LogFile.Write();
	LogFile.AddLine(_(""));
	for(int i=0;i<NumberOfChannels;i++)
	{
		LogFile.AddLine(pCHLabels[i]);	
	}
	LogFile.AddLine(_(""));
	LogFile.Write();
}
void CDatSaver::FileOpen(wxString Filename,wxString Path,wxString LogText)
{

	wxString	Temp;
	wxDateTime	Time;

	AlgoFileName = DatFileName = BinFileName = LogFileName = Path + Filename;

	BinFileName.Replace(_T(".dat"),_T(".bin"));
	LogFileName.Replace(_T(".dat"),_T(".txt"));
	AlgoFileName.Replace(_T(".dat"),_T("_Algo.dat"));
	
	if(!BinFile.Create(BinFileName,true)||!DatFile.Create(DatFileName)||!LogFile.Create(LogFileName))
	{
			IsClosed	=	true;
	}else
	{
		Time.SetToCurrent();
		Temp = _T("Acquisition start: ") + Time.FormatISODate() + _T("   ") + Time.FormatISOTime();
		LogFile.AddLine(Temp);
		LogFile.Write();
		IsClosed	=	false;
	}
}



void CDatSaver::SaveData(SampleStruct *pData, UINT Dim, int offset)
{

	unsigned long Scritti = 0;
	if(IsClosed == true) return;
	int Len = sizeof(SampleStruct);
	Scritti = BinFile.Write(&pData->Data.DataLen,(unsigned long)(sizeof(int)));
	if(Scritti == 0) wxMessageBox(_("Error writing to disk"));
	BinFile.Write(pData->Data.Signal,(unsigned long)(pData->Data.DataLen * sizeof(float)));
	BinFile.Write(&pData->Data.AUX,(unsigned long)(sizeof(float)));
	BinFile.Write(&pData->Data.Artifacts,(unsigned long)(sizeof(int)));
	BinFile.Write(&pData->Algo,(unsigned long)(sizeof(AlgoStruct)));
	Scritti = BinFile.Write(&pData->Events,(unsigned long)(sizeof(EventStruct)));
	if(Scritti == 0) wxMessageBox(_("Error writing to disk"));
}
//#define BLOCCO_DATI		250
void CDatSaver::Close()
{
	char		Line[10098];
	char		AlgoLine[1024];
	wxDateTime	Time;
	wxString	Temp = _T("");
	SampleStruct	Dati;
	unsigned long Dim = 0;
	wxCriticalSection Region;
	if(IsClosed) return;

	//Aggiorno i file di log
	Time.SetToCurrent();
	Temp = _T("Acquisition stop: ") + Time.FormatISODate() + _T("   ") + Time.FormatISOTime();
	LogFile.AddLine(Temp);
	LogFile.Write();
	
	Region.Enter();
	BinFile.Close();
	BinFile.Open(BinFileName);

	Dim = BinFile.Read(&Dati.Data.DataLen,sizeof(Dati.Data.DataLen));
	if(Dim >0)
	{
		BinFile.Read(Dati.Data.Signal,Dati.Data.DataLen*sizeof(float));
		BinFile.Read(&Dati.Data.AUX,sizeof(Dati.Data.AUX));
		BinFile.Read(&Dati.Data.Artifacts,sizeof(Dati.Data.Artifacts));
		BinFile.Read(&Dati.Algo,sizeof(AlgoStruct));
		BinFile.Read(&Dati.Events,sizeof(EventStruct));
		if(Dati.Algo.FeedLen>0)
		{
			AlgoFile.Create(AlgoFileName);		
		}
	}

	do
	{
		Dim = BinFile.Read(&Dati.Data.DataLen,sizeof(Dati.Data.DataLen));
		if(Dim >0)
		{
			BinFile.Read(Dati.Data.Signal,Dati.Data.DataLen*sizeof(float));
			BinFile.Read(&Dati.Data.AUX,sizeof(Dati.Data.AUX));
			BinFile.Read(&Dati.Data.Artifacts,sizeof(Dati.Data.Artifacts));
			BinFile.Read(&Dati.Algo,sizeof(AlgoStruct));
			BinFile.Read(&Dati.Events,sizeof(EventStruct));
			
			sprintf(Line,"");
			for(int i=0;i<Dati.Data.DataLen;i++)
			{
				sprintf(Line,"%s%f\t",Line,Dati.Data.Signal[i]);
			}
			if(Dati.Algo.Classification != 0)
			{
				int i = 0;
			
			}
			sprintf(Line,"%s%d\t%d\t%d\t%f\t",Line,Dati.Events.AEnimaTrigg,Dati.Events.HIMTrigg,Dati.Events.HWTrigger,Dati.Algo.Classification);
			
			if(AlgoFile.IsOpened())
			{
				sprintf(AlgoLine,"%.0f\t",Dati.Algo.Classification);
				for(int i=0;i<Dati.Algo.FeedLen;i++)
				{
					sprintf(AlgoLine,"%s%.1f\t",AlgoLine,Dati.Algo.Feedback[i]);
				}
				sprintf(AlgoLine,"%s\n",AlgoLine);
				AlgoFile.Write(AlgoLine,strlen(AlgoLine));
			}
			

			sprintf(Line,"%s%f\t%d\n",Line,Dati.Data.AUX,Dati.Events.CommunicationStats);
			DatFile.Write(Line,strlen(Line));
		}
	}while(Dim > 0);
//	DatFile.Write();
	BinFile.Close();
	DatFile.Close();
	LogFile.Close();
	if (AlgoFile.IsOpened()) AlgoFile.Close();
	IsClosed	=	true;
	Region.Leave();
}
